package com.gameguru.mathgame;

import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;

public class ShowChart extends Activity {
	DBHelper dbHelper;
	GameHelper gameHelper;
	XYMultipleSeriesDataset dataset;
	private boolean isNoAddData = false;
	private boolean isNoSubData = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbHelper = new DBHelper(this);
		gameHelper = new GameHelper(this);
		isNoAddData = false;
		isNoSubData = false;
		dataset = getDataset();
	/*	try {
			dataset = getDataset();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			isNoAddData = true;
			isNoSubData = true;
		}*/
		if (isNoAddData&&isNoSubData) {
			Builder alert = new Builder(this);
			alert.setMessage(getString(R.string.no_data_to_chart));
			alert.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					finish();
					
				}
			});
			alert.setNegativeButton(getString(R.string.ok_go_back), new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alert.setPositiveButton(getString(R.string.show_the_sample_chart),
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = ChartFactory.getTimeChartIntent(
									getApplicationContext(), getDemoDataset(),
									getRenderer(), null);
							startActivity(intent);
							finish();
						}
					});
			alert.show();
		} else {
			Intent intent = ChartFactory.getLineChartIntent(this, dataset,
					getRenderer());
			startActivity(intent);
			finish();
		}
	}

	private XYMultipleSeriesDataset getDataset() {
		dataset = new XYMultipleSeriesDataset();
		Cursor addCursor = dbHelper.getAllReport(gameHelper.getPlayerName(),
				Constants.ADDITION);
		TimeSeries series1 = new TimeSeries(getString(R.string.add));
		if (addCursor.getCount() > 0) {
			while (!addCursor.isAfterLast()) {
				int mark = getMark((addCursor.getInt(addCursor
						.getColumnIndex(DBHelper.SCORE))), addCursor.getInt(addCursor.getColumnIndex(DBHelper.TOT_Q)));
				series1.add(addCursor.getPosition(), mark);
				addCursor.moveToNext();
			}
		} else {
			isNoAddData = true;
		}

		Cursor subCursor = dbHelper.getAllReport(gameHelper.getPlayerName(),
				Constants.SUBTRACTION);
		TimeSeries series2 = new TimeSeries(getString(R.string.subtraction));
		if (subCursor.getCount() > 0) {
			while (!subCursor.isAfterLast()) {
				int mark = getMark((subCursor.getInt(subCursor
						.getColumnIndex(DBHelper.SCORE))), subCursor.getInt(subCursor.getColumnIndex(DBHelper.TOT_Q)));
				series2.add(subCursor.getPosition(), mark);
				subCursor.moveToNext();
			}

		} else {
			isNoSubData = true;
		}
		dataset.addSeries(series1);
		dataset.addSeries(series2);
		return dataset;
	}

	private XYMultipleSeriesRenderer getRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[] { 20, 30, 15, 0 });
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(Color.BLUE);
		r.setPointStyle(PointStyle.SQUARE);
		r.setFillBelowLine(false);
		r.setFillBelowLineColor(Color.WHITE);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		r = new XYSeriesRenderer();
		r.setPointStyle(PointStyle.CIRCLE);
		r.setColor(Color.GREEN);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.DKGRAY);
		renderer.setLabelsColor(Color.LTGRAY);
		return renderer;
	}

	private XYMultipleSeriesDataset getDemoDataset() {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		final int nr = 10;
		Random r = new Random();
		for (int i = 0; i < 2; i++) {// Two series
			XYSeries series = new XYSeries(getString(R.string.sample_report) +" "+ (i + 1));
			for (int k = 0; k < nr; k++) {
				series.add(k, 20 + r.nextInt() % 100);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}
	
	private int getMark(int i, int j) {
		int mark = 0;
		try {
			if(j!=0)
			mark = (i*100)/j;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return mark;
	}
}
